﻿using Microsoft.SharePoint.Client;
using SPCoder.Utils.Nodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace SPCoder.Utils
{
    public class CSOMConnector : BaseConnector
    {
        private string username;
        private string password;
        
        public CSOMConnector(string username, string password)
        {
            this.username = username;
            this.password = password;        
        }

        public override BaseNode GetSPStructure(string siteUrl)
        {
            ClientContext context = new ClientContext(siteUrl);
            if (this.AuthenticationType == SPCoderConstants.O365)
            {
                SecureString pass = new SecureString();
                foreach (char c in password.ToCharArray()) pass.AppendChar(c);
                context.Credentials = new SharePointOnlineCredentials(username, pass);
            }
            else if (this.AuthenticationType == SPCoderConstants.FBA)
            {
                context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
                context.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(username, password);
            }
            else if (this.AuthenticationType == SPCoderConstants.WIN)
            {
                context.AuthenticationMode = ClientAuthenticationMode.Default;
                context.Credentials = new NetworkCredential(username, password);                    
            }

            Site site = context.Site;
            context.Load(site);
            context.ExecuteQuery();
            BaseNode rootNode = new SiteNode(site);
            rootNode.OMType = ObjectModelType.CSOM;
            rootNode.SPObject = site;
            doSPWeb(site.RootWeb, rootNode, rootNode);

            return rootNode;
        }

        private void doSPWeb(Web web, BaseNode parentNode, BaseNode rootNode)
        {
            try
            {
                BaseNode myNode = new WebNode(web);
                parentNode.Children.Add(myNode);
                myNode.ParentNode = parentNode;
                myNode.RootNode = rootNode;
                web.Context.Load(web.Webs);
                web.Context.ExecuteQuery();
                try
                {
                    foreach (Web childWeb in web.Webs)
                    {
                        doSPWeb(childWeb, myNode, rootNode);
                    }
                }
                catch (Exception exc)
                {
                    SPCoderForm.MainForm.LogException(exc);
                }

                web.Context.Load(web.Lists);
                web.Context.ExecuteQuery();
                foreach (List list in web.Lists)
                {
                    web.Context.Load(list);
                }
                web.Context.ExecuteQuery();
                foreach (List list in web.Lists)
                {
                    BaseNode myListNode = new ListNode(list);
                    myNode.Children.Add(myListNode);
                    myListNode.ParentNode = myNode;
                    myListNode.RootNode = rootNode;
                }
                //web.Dispose();
            }
            catch (Exception exc)
            {
                SPCoderForm.MainForm.LogException(exc);
            }
        }
    }
}
